In [1]:
from IPython.display import display, HTML

display(HTML(data="""
<style>
    div#notebook-container    { width: 95%; }
    div#menubar-container     { width: 65%; }
    div#maintoolbar-container { width: 99%; }a
</style>
"""))

Assigment 7¶

7.1 Install libraries if not already installed¶

In [2]:
#!pip install geopandas
#!pip install folium 
#!pip install -U googlemaps
#!pip install plotly
In [3]:
import pandas as pd
from pandas import Series, DataFrame
import numpy as np
import matplotlib.pyplot as plt 
import chardet
In [4]:
import geopandas as gpd
from geopandas import GeoSeries
from shapely.geometry import Point, LineString
import folium 
from folium import Marker, GeoJson
from folium.plugins import MarkerCluster, HeatMap
In [5]:
from scipy.spatial import distance
In [6]:
import googlemaps
from datetime import datetime
In [7]:
import plotly.express as px

7.2. Import the data located at this link. This dataset comes from this link. It has information on all the geolocated health establishments in Cajamarca.¶

In [8]:
# Read data from shared excel
cajamarca_helth_estb = pd.read_excel( r"../../_data/cajamarca_data_helth_estb.xlsx" )
cajamarca_helth_estb
Out[8]:
id_eess codigo_renaes categoria nombre diresa red direccion longitud latitud id_ubigeo ... CCPP NOMBPROV CCDI NOMBDIST CAPITAL UBIGEO IDPROV CODIGO CNT_CCPP DESCRIPCIO
0 9439 4210 II-1 GRAL. JAEN CAJAMARCA NO PERTENECE A NINGUNA RED AVENIDA AV. PAKAMAUROS S/N S/N AV. PAKAMAUROS ... -78.804711 -5.706214 636 ... 8 JAEN 1 JAEN JAEN 60801 608 60801 108 JAEN
1 27242 4211 I-4 MORRO SOLAR CAJAMARCA JAEN OTROS ALFREDO BASTOS Nº 630 ALFREDO BASTOS Nº ... -78.811920 -5.710600 636 ... 8 JAEN 1 JAEN JAEN 60801 608 60801 108 JAEN
2 22911 4212 I-3 MAGLLANAL CAJAMARCA JAEN CALLE CALLE LAS ALMENDRA CDRA. 4 S/N S/N CALLE... -78.817266 -5.707647 636 ... 8 JAEN 1 JAEN JAEN 60801 608 60801 108 JAEN
3 9242 4213 I-3 FILA ALTA CAJAMARCA JAEN OTROS COMUNIDAD FILA ALTA COMUNIDAD FILA ALTA ... -78.791643 -5.739184 636 ... 8 JAEN 1 JAEN JAEN 60801 608 60801 108 JAEN
4 26418 4214 I-1 LAS NARANJAS CAJAMARCA JAEN OTROS COMUNIDAD DE LAS NARANJAS COMUNIDAD DE L... -78.846347 -5.745295 636 ... 8 JAEN 1 JAEN JAEN 60801 608 60801 108 JAEN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1187 21625 5038 I-3 SAN LUIS DE LA LUCMA CAJAMARCA SOCOTA CARRETERA CARRETERA LUCMA SOCOTA DISTRITO SAN ... -78.607471 -6.292044 627 ... 6 CUTERVO 10 SAN LUIS DE LUCMA SAN LUIS DE LUCMA 60610 606 60610 19 SAN LUIS DE LUCMA
1188 8179 6853 I-1 SAIREPAMPA CAJAMARCA SOCOTA OTROS COMUNIDAD DE SAIREPAMPA DISTRITO SAN LUI... -78.631080 -6.279960 627 ... 6 CUTERVO 10 SAN LUIS DE LUCMA SAN LUIS DE LUCMA 60610 606 60610 19 SAN LUIS DE LUCMA
1189 16451 6870 I-1 SANTA ROSA DEL TINGO CAJAMARCA SOCOTA OTROS COMUNIDAD DE SANTA ROSA DEL TINGO DISTRI... -78.597640 -6.266560 627 ... 6 CUTERVO 10 SAN LUIS DE LUCMA SAN LUIS DE LUCMA 60610 606 60610 19 SAN LUIS DE LUCMA
1190 3040 7048 I-1 SEXESHITA CAJAMARCA SOCOTA OTROS COMUNIDAD DE SEXESHITA DISTRITO SAN LUIS... -78.627210 -6.267210 627 ... 6 CUTERVO 10 SAN LUIS DE LUCMA SAN LUIS DE LUCMA 60610 606 60610 19 SAN LUIS DE LUCMA
1191 26156 6995 I-1 PISAGUAS CAJAMARCA SAN IGNACIO OTROS COMUNIDAD DE PISAGUAS DISTRITO HUARANGO ... -78.640093 -5.248693 650 ... 2 BAGUA 5 IMAZA CHIRIACO 10205 102 10205 159 IMAZA

1192 rows × 23 columns

7.3 Import the district's centroids data. It has the latitude and longitude of every district in Peru.¶

In [9]:
# Read data from shared excel
districts_centroids = pd.read_excel( r"../../_data/peru_districts_centroids.xlsx" )
districts_centroids 
Out[9]:
CCDD NOMBDEP CCPP NOMBPROV CCDI NOMBDIST CAPITAL UBIGEO IDPROV CODIGO Centroid_Latitude Centroid_Longitude
0 6 CAJAMARCA 5 CONTUMAZA 4 GUZMANGO GUZMANGO 60504 605 60504 -7.364304 -78.917689
1 6 CAJAMARCA 11 SAN MIGUEL 5 EL PRADO EL PRADO 61105 611 61105 -7.038039 -79.003318
2 6 CAJAMARCA 11 SAN MIGUEL 9 NIEPOS NIEPOS 61109 611 61109 -6.926718 -79.131435
3 6 CAJAMARCA 11 SAN MIGUEL 10 SAN GREGORIO SAN GREGORIO 61110 611 61110 -7.117978 -79.160968
4 6 CAJAMARCA 12 SAN PABLO 3 SAN LUIS SAN LUIS GRANDE 61203 612 61203 -7.159046 -78.873084
... ... ... ... ... ... ... ... ... ... ... ... ...
1869 4 AREQUIPA 4 CASTILLA 4 CHACHAS CHACHAS 40404 404 40404 -15.181923 -72.116054
1870 4 AREQUIPA 4 CASTILLA 5 CHILCAYMARCA CHILCAYMARCA 40405 404 40405 -15.350613 -72.385276
1871 4 AREQUIPA 4 CASTILLA 6 CHOCO CHOCO 40406 404 40406 -15.414004 -72.068684
1872 4 AREQUIPA 4 CASTILLA 7 HUANCARQUI HUANCARQUI 40407 404 40407 -16.012364 -72.368167
1873 12 JUNIN 6 SATIPO 4 MAZAMARI NaN 120604 1206 120604 -11.493927 -74.241229

1874 rows × 12 columns

7.4 Use these datasets and find the closest health establishment to Cajamarca district centroids. Use euclidian distance between points. DO NOT USE GOOGLE API. IT WILL BE VERY EXPENSIVE For each district, there should be only one health establishment.¶

7.4.1 Exploration¶

We observe that the key column is UBIGEO

In [10]:
cajamarca_helth_estb.columns
Out[10]:
Index(['id_eess', 'codigo_renaes', 'categoria', 'nombre', 'diresa', 'red',
       'direccion', 'longitud', 'latitud', 'id_ubigeo', 'ind', 'CCDD',
       'NOMBDEP', 'CCPP', 'NOMBPROV', 'CCDI', 'NOMBDIST', 'CAPITAL', 'UBIGEO',
       'IDPROV', 'CODIGO', 'CNT_CCPP', 'DESCRIPCIO'],
      dtype='object')
In [11]:
districts_centroids.columns
Out[11]:
Index(['CCDD', 'NOMBDEP', 'CCPP', 'NOMBPROV', 'CCDI', 'NOMBDIST', 'CAPITAL',
       'UBIGEO', 'IDPROV', 'CODIGO', 'Centroid_Latitude',
       'Centroid_Longitude'],
      dtype='object')

7.4.2 Database Join¶

According to the instructions, for the "districts_centroids" base, we keep the 'UBIGEO' (key) column, along with 'Centroid_Latitude' and 'Centroid_Longitude' columns.

In [12]:
# The merge is performed using 'UBIGEO' as the key
merged_data = cajamarca_helth_estb.merge(districts_centroids[['UBIGEO', 'Centroid_Latitude', 'Centroid_Longitude']],
                                         on='UBIGEO', 
                                         how='inner')
In [13]:
merged_data
Out[13]:
id_eess codigo_renaes categoria nombre diresa red direccion longitud latitud id_ubigeo ... CCDI NOMBDIST CAPITAL UBIGEO IDPROV CODIGO CNT_CCPP DESCRIPCIO Centroid_Latitude Centroid_Longitude
0 9439 4210 II-1 GRAL. JAEN CAJAMARCA NO PERTENECE A NINGUNA RED AVENIDA AV. PAKAMAUROS S/N S/N AV. PAKAMAUROS ... -78.804711 -5.706214 636 ... 1 JAEN JAEN 60801 608 60801 108 JAEN -5.770179 -78.852551
1 27242 4211 I-4 MORRO SOLAR CAJAMARCA JAEN OTROS ALFREDO BASTOS Nº 630 ALFREDO BASTOS Nº ... -78.811920 -5.710600 636 ... 1 JAEN JAEN 60801 608 60801 108 JAEN -5.770179 -78.852551
2 22911 4212 I-3 MAGLLANAL CAJAMARCA JAEN CALLE CALLE LAS ALMENDRA CDRA. 4 S/N S/N CALLE... -78.817266 -5.707647 636 ... 1 JAEN JAEN 60801 608 60801 108 JAEN -5.770179 -78.852551
3 9242 4213 I-3 FILA ALTA CAJAMARCA JAEN OTROS COMUNIDAD FILA ALTA COMUNIDAD FILA ALTA ... -78.791643 -5.739184 636 ... 1 JAEN JAEN 60801 608 60801 108 JAEN -5.770179 -78.852551
4 26418 4214 I-1 LAS NARANJAS CAJAMARCA JAEN OTROS COMUNIDAD DE LAS NARANJAS COMUNIDAD DE L... -78.846347 -5.745295 636 ... 1 JAEN JAEN 60801 608 60801 108 JAEN -5.770179 -78.852551
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1187 21625 5038 I-3 SAN LUIS DE LA LUCMA CAJAMARCA SOCOTA CARRETERA CARRETERA LUCMA SOCOTA DISTRITO SAN ... -78.607471 -6.292044 627 ... 10 SAN LUIS DE LUCMA SAN LUIS DE LUCMA 60610 606 60610 19 SAN LUIS DE LUCMA -6.279101 -78.578753
1188 8179 6853 I-1 SAIREPAMPA CAJAMARCA SOCOTA OTROS COMUNIDAD DE SAIREPAMPA DISTRITO SAN LUI... -78.631080 -6.279960 627 ... 10 SAN LUIS DE LUCMA SAN LUIS DE LUCMA 60610 606 60610 19 SAN LUIS DE LUCMA -6.279101 -78.578753
1189 16451 6870 I-1 SANTA ROSA DEL TINGO CAJAMARCA SOCOTA OTROS COMUNIDAD DE SANTA ROSA DEL TINGO DISTRI... -78.597640 -6.266560 627 ... 10 SAN LUIS DE LUCMA SAN LUIS DE LUCMA 60610 606 60610 19 SAN LUIS DE LUCMA -6.279101 -78.578753
1190 3040 7048 I-1 SEXESHITA CAJAMARCA SOCOTA OTROS COMUNIDAD DE SEXESHITA DISTRITO SAN LUIS... -78.627210 -6.267210 627 ... 10 SAN LUIS DE LUCMA SAN LUIS DE LUCMA 60610 606 60610 19 SAN LUIS DE LUCMA -6.279101 -78.578753
1191 26156 6995 I-1 PISAGUAS CAJAMARCA SAN IGNACIO OTROS COMUNIDAD DE PISAGUAS DISTRITO HUARANGO ... -78.640093 -5.248693 650 ... 5 IMAZA CHIRIACO 10205 102 10205 159 IMAZA -4.991267 -78.389595

1192 rows × 25 columns

7.4.3 Euclidian distance between points¶

In [14]:
# Create a function to calculate the Euclidean distance
def calcular_distancia(coord1, coord2):
    return np.linalg.norm(np.array(coord1) - np.array(coord2))

# Create a new column in the DataFrame with the Euclidean distance
merged_data["distancia"] = merged_data.apply(lambda row: calcular_distancia((row['latitud'], row['longitud']), 
                                                                             (row['Centroid_Latitude'], row['Centroid_Longitude'])),
                                             axis=1)

# Create a column to indicate if the distance is minimum
merged_data["min"] = 'No'

# Use groupby and transform to find the minimum distance per district
merged_data['min_distancia'] = merged_data.groupby('NOMBDIST')['distancia'].transform('min')

# Mark 'Yes' in 'min' when the distance is equal to the minimum per district
merged_data.loc[merged_data['distancia'] == merged_data['min_distancia'], 'min'] = 'Sí'

# Remove the auxiliary column 'min_distance'
merged_data.drop(columns=['min_distancia'], inplace=True)

# Create a new DataFrame with rows where 'min' is equal to 'Yes'
merged_data_2 = merged_data.loc[merged_data['min'] == 'Sí', :]
merged_data_2  # Display the resulting DataFrame with minimum distances
Out[14]:
id_eess codigo_renaes categoria nombre diresa red direccion longitud latitud id_ubigeo ... CAPITAL UBIGEO IDPROV CODIGO CNT_CCPP DESCRIPCIO Centroid_Latitude Centroid_Longitude distancia min
4 26418 4214 I-1 LAS NARANJAS CAJAMARCA JAEN OTROS COMUNIDAD DE LAS NARANJAS COMUNIDAD DE L... -78.846347 -5.745295 636 ... JAEN 60801 608 60801 108 JAEN -5.770179 -78.852551 0.025645 Sí
120 24900 4230 I-1 SAN AUGUSTIN CAJAMARCA JAEN CALLE CALLE IVA S/N S/N CALLE IVA S/N BELLAVIS... -78.793840 -5.585960 637 ... BELLAVISTA 60802 608 60802 77 BELLAVISTA -5.564669 -78.742625 0.055464 Sí
123 17284 4233 I-2 PACHAPIRIANA CAJAMARCA JAEN OTROS COMUNIDAD DE PACHAPIRIANA COMUNIDAD DE P... -79.126397 -5.640227 638 ... CHONTALI 60803 608 60803 56 CHONTALI -5.633938 -79.133355 0.009379 Sí
131 1918 7017 I-2 AHUYACA CAJAMARCA JAEN OTROS C.P AHUYACA C.P AHUYACA COLASAY JAEN CAJ... -79.002566 -5.935045 639 ... COLASAY 60804 608 60804 122 COLASAY -5.901052 -78.979456 0.041104 Sí
135 11525 4241 I-3 HUABAL CAJAMARCA JAEN OTROS COMUNIDAD DE HUABAL COMUNIDAD DE HUABAL ... -78.899635 -5.612133 640 ... HUABAL 60805 608 60805 30 HUABAL -5.604925 -78.905854 0.009519 Sí
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1163 13976 7179 I-1 CHACRERIAS CAJAMARCA SOCOTA OTROS COMUNIDAD DE CHACRERIAS COMUNIDAD DE CHA... -78.577313 -6.229452 622 ... LA RAMADA 60605 606 60605 17 LA RAMADA -6.230898 -78.569699 0.007751 Sí
1170 26339 7103 I-1 SANTA ROSA DE SAN JUAN CAJAMARCA SOCOTA OTROS COMUNIDAD DE SANTA ROSA COMUNIDAD DE SAN... -78.603620 -6.171020 626 ... SAN JUAN DE CUTERVO 60609 606 60609 23 SAN JUAN DE CUTERVO -6.174218 -78.600092 0.004761 Sí
1179 8592 6937 I-1 MINAS DE SOCOTA CAJAMARCA SOCOTA OTROS COMUNIDAD DE MINAS COMUNIDAD DE MINAS SO... -78.690430 -6.292890 631 ... SOCOTA 60614 606 60614 37 SOCOTA -6.287362 -78.696719 0.008373 Sí
1189 16451 6870 I-1 SANTA ROSA DEL TINGO CAJAMARCA SOCOTA OTROS COMUNIDAD DE SANTA ROSA DEL TINGO DISTRI... -78.597640 -6.266560 627 ... SAN LUIS DE LUCMA 60610 606 60610 19 SAN LUIS DE LUCMA -6.279101 -78.578753 0.022672 Sí
1191 26156 6995 I-1 PISAGUAS CAJAMARCA SAN IGNACIO OTROS COMUNIDAD DE PISAGUAS DISTRITO HUARANGO ... -78.640093 -5.248693 650 ... CHIRIACO 10205 102 10205 159 IMAZA -4.991267 -78.389595 0.359190 Sí

129 rows × 27 columns

7.4.4 Check if each district has only one health facility assigned¶

In [15]:
# Count the number of health establishments per district
count_per_district = merged_data_2.groupby('NOMBDIST')['codigo_renaes'].count()

# Filter districts with more than one health establishment
distritos_con_mas_de_un_establecimiento = count_per_district[count_per_district > 1]

# Display districts with more than one health establishment
print("Distritos con más de un establecimiento de salud:")
print(distritos_con_mas_de_un_establecimiento)

# Check if there is any district with more than one establishment
if distritos_con_mas_de_un_establecimiento.empty:
    print("Para cada distrito, hay solo un establecimiento de salud.")
else:
    print("Hay distritos con más de un establecimiento de salud")
Distritos con más de un establecimiento de salud:
NOMBDIST
PEDRO GALVEZ    2
Name: codigo_renaes, dtype: int64
Hay distritos con más de un establecimiento de salud
In [16]:
# Filter rows corresponding to the district with more than one health establishment
distrito_observado = 'PEDRO GALVEZ'  
filas_distrito_observado = merged_data_2[merged_data_2['NOMBDIST'] == distrito_observado]

filas_distrito_observado
Out[16]:
id_eess codigo_renaes categoria nombre diresa red direccion longitud latitud id_ubigeo ... CAPITAL UBIGEO IDPROV CODIGO CNT_CCPP DESCRIPCIO Centroid_Latitude Centroid_Longitude distancia min
360 23556 28974 I-3 CENTRO MEDICO SAN MARCOS TU SALUD EN NUESTRAS ... CAJAMARCA NO PERTENECE A NINGUNA RED JIRÓN INCLAN NÚMERO 124 PISO 1 DISTRITO PEDRO ... -78.170694 -7.332226 655 ... SAN MARCOS 61001 610 61001 58 PEDRO GALVEZ -7.329159 -78.171237 0.003115 Sí
361 9785 29005 I-1 CONSULTORIO ODONTOLOGICO SAN MARCOS CAJAMARCA NO PERTENECE A NINGUNA RED JIRÓN LUNA NÚMERO 346 PISO 1 DISTRITO PEDRO GA... -78.170694 -7.332226 655 ... SAN MARCOS 61001 610 61001 58 PEDRO GALVEZ -7.329159 -78.171237 0.003115 Sí

2 rows × 27 columns

The establishments "CENTRO MEDICO SAN MARCOS TU SALUD EN NUESTRAS MANOS EIRL" and "CONSULTORIO ODONTOLOGICO SAN MARCOS" located in the Pedro Gálvez district share the same latitude and longitude. Consequently, the Euclidean distance between them is practically zero, classifying them as duplicates for that district. In this regard, the decision has been made not to remove the data, and therefore, this information will be retained in the ongoing analysis.

7.5 Use Google API Directions to find the driving travel time and distance from health establishments to district centroids. Set driving as the value of the parameter mode and Monday 4pm as departure_time. Include information on the three options of the traffic_model variable: best_guess, pessimistic, and optimistic¶

In [17]:
# We explore the column names
merged_data_2.columns
Out[17]:
Index(['id_eess', 'codigo_renaes', 'categoria', 'nombre', 'diresa', 'red',
       'direccion', 'longitud', 'latitud', 'id_ubigeo', 'ind', 'CCDD',
       'NOMBDEP', 'CCPP', 'NOMBPROV', 'CCDI', 'NOMBDIST', 'CAPITAL', 'UBIGEO',
       'IDPROV', 'CODIGO', 'CNT_CCPP', 'DESCRIPCIO', 'Centroid_Latitude',
       'Centroid_Longitude', 'distancia', 'min'],
      dtype='object')

7.5.1 Rename columns:¶

The final data frame should have only the following columns: id_eess, codigo_renaes, categoria, nombre, diresa, red, direccion, longitud, latitud, id_ubigeo, UBIGEO, CCDD, NOMBDEP, CCPP, NOMBPROV, CCDI, NOMBDIST, CAPITAL, Dist_Centroid_Latitude, Dist_Centroid_Longitude, travel_time_best_guess, travel_time_pessimistic, travel_time_optimistic, travel_distance_best_guess, travel_distance_pessimistic, travel_distance_optimistic.

Based on the instructions, the variables Centroid_Latitude and Centroid_Longitude are renamed.

In [18]:
merged_data_2 = merged_data_2.copy()  # Create an explicit copy of the DataFrame
merged_data_2.rename(columns={'Centroid_Latitude': 'Dist_Centroid_Latitude', 'Centroid_Longitude': 'Dist_Centroid_Longitude'}, inplace=True)
merged_data_2.columns
Out[18]:
Index(['id_eess', 'codigo_renaes', 'categoria', 'nombre', 'diresa', 'red',
       'direccion', 'longitud', 'latitud', 'id_ubigeo', 'ind', 'CCDD',
       'NOMBDEP', 'CCPP', 'NOMBPROV', 'CCDI', 'NOMBDIST', 'CAPITAL', 'UBIGEO',
       'IDPROV', 'CODIGO', 'CNT_CCPP', 'DESCRIPCIO', 'Dist_Centroid_Latitude',
       'Dist_Centroid_Longitude', 'distancia', 'min'],
      dtype='object')

7.5.2 Creation of the 'establish_data' dictionary from the merged_data_2 dataFrame:¶

In [19]:
establish_data = merged_data_2[["nombre","longitud","latitud","UBIGEO","Dist_Centroid_Latitude","Dist_Centroid_Longitude"]].to_dict(orient="index")
establish_data
Out[19]:
{4: {'nombre': 'LAS NARANJAS',
  'longitud': -78.846347,
  'latitud': -5.745295,
  'UBIGEO': 60801,
  'Dist_Centroid_Latitude': -5.770178526822774,
  'Dist_Centroid_Longitude': -78.85255115664602},
 120: {'nombre': 'SAN AUGUSTIN',
  'longitud': -78.79384,
  'latitud': -5.58596,
  'UBIGEO': 60802,
  'Dist_Centroid_Latitude': -5.564668861888495,
  'Dist_Centroid_Longitude': -78.74262548286006},
 123: {'nombre': 'PACHAPIRIANA',
  'longitud': -79.12639667,
  'latitud': -5.64022668,
  'UBIGEO': 60803,
  'Dist_Centroid_Latitude': -5.633938366176614,
  'Dist_Centroid_Longitude': -79.1333554620538},
 131: {'nombre': 'AHUYACA',
  'longitud': -79.00256579,
  'latitud': -5.93504498,
  'UBIGEO': 60804,
  'Dist_Centroid_Latitude': -5.901052025899031,
  'Dist_Centroid_Longitude': -78.97945638518134},
 135: {'nombre': 'HUABAL',
  'longitud': -78.89963505,
  'latitud': -5.61213259,
  'UBIGEO': 60805,
  'Dist_Centroid_Latitude': -5.604925456320518,
  'Dist_Centroid_Longitude': -78.90585415967001},
 150: {'nombre': 'SAN PEDRO',
  'longitud': -79.03845599,
  'latitud': -5.54900682,
  'UBIGEO': 60811,
  'Dist_Centroid_Latitude': -5.517367642855747,
  'Dist_Centroid_Longitude': -79.05506108087833},
 154: {'nombre': 'LAS PIRIAS DE JAEN',
  'longitud': -78.8524157,
  'latitud': -5.62707293,
  'UBIGEO': 60806,
  'Dist_Centroid_Latitude': -5.636341962042768,
  'Dist_Centroid_Longitude': -78.84958991537201},
 158: {'nombre': 'MANGAYPA',
  'longitud': -79.17677956,
  'latitud': -5.87976212,
  'UBIGEO': 60807,
  'Dist_Centroid_Latitude': -5.8753999738952,
  'Dist_Centroid_Longitude': -79.19500591545217},
 164: {'nombre': 'POSTA MÉDICA DE PUCARÁ',
  'longitud': -79.1297751,
  'latitud': -6.0418671,
  'UBIGEO': 60808,
  'Dist_Centroid_Latitude': -6.039986978158075,
  'Dist_Centroid_Longitude': -79.12365551040568},
 165: {'nombre': 'SALLIQUE',
  'longitud': -79.31577965,
  'latitud': -5.65620348,
  'UBIGEO': 60809,
  'Dist_Centroid_Latitude': -5.625686059091744,
  'Dist_Centroid_Longitude': -79.31242741575443},
 172: {'nombre': 'SAN FELIPE',
  'longitud': -79.31283463,
  'latitud': -5.77025781,
  'UBIGEO': 60810,
  'Dist_Centroid_Latitude': -5.786477227250232,
  'Dist_Centroid_Longitude': -79.31021142229889},
 176: {'nombre': 'PUENTECILLOS',
  'longitud': -78.58698,
  'latitud': -5.41009,
  'UBIGEO': 60812,
  'Dist_Centroid_Latitude': -5.427733348514218,
  'Dist_Centroid_Longitude': -78.60084336623758},
 181: {'nombre': 'SAN IGNACIO',
  'longitud': -79.003002,
  'latitud': -5.14467,
  'UBIGEO': 60901,
  'Dist_Centroid_Latitude': -5.140405077923568,
  'Dist_Centroid_Longitude': -79.00789460996974},
 205: {'nombre': 'PAMPA VERDE',
  'longitud': -79.17658,
  'latitud': -5.08898,
  'UBIGEO': 60905,
  'Dist_Centroid_Latitude': -5.107316398456057,
  'Dist_Centroid_Longitude': -79.18098681854407},
 210: {'nombre': 'CHIRINOS',
  'longitud': -78.90172426,
  'latitud': -5.30277342,
  'UBIGEO': 60902,
  'Dist_Centroid_Latitude': -5.307553228839856,
  'Dist_Centroid_Longitude': -78.88245718685589},
 223: {'nombre': 'EL PORVENIR DE HUARANGO',
  'longitud': -78.75058,
  'latitud': -5.16398,
  'UBIGEO': 60903,
  'Dist_Centroid_Latitude': -5.09652405211401,
  'Dist_Centroid_Longitude': -78.71529057820497},
 232: {'nombre': 'RUMIPITE',
  'longitud': -78.97517,
  'latitud': -5.38766,
  'UBIGEO': 60904,
  'Dist_Centroid_Latitude': -5.38963741943043,
  'Dist_Centroid_Longitude': -78.97348368601949},
 256: {'nombre': 'DIAMANTE',
  'longitud': -78.90137,
  'latitud': -4.92764,
  'UBIGEO': 60906,
  'Dist_Centroid_Latitude': -4.915990503475814,
  'Dist_Centroid_Longitude': -78.83184908839714},
 263: {'nombre': 'TAMBORAPA PUEBLO',
  'longitud': -79.15838419,
  'latitud': -5.29116357,
  'UBIGEO': 60907,
  'Dist_Centroid_Latitude': -5.324338543193906,
  'Dist_Centroid_Longitude': -79.21511925434639},
 273: {'nombre': 'SAN ANTONIO',
  'longitud': -78.13502445,
  'latitud': -6.80404279,
  'UBIGEO': 60301,
  'Dist_Centroid_Latitude': -6.774938931037327,
  'Dist_Centroid_Longitude': -78.11727136589845},
 287: {'nombre': 'UTCO LIMON',
  'longitud': -78.09005808,
  'latitud': -6.87100658,
  'UBIGEO': 60311,
  'Dist_Centroid_Latitude': -6.881324985969411,
  'Dist_Centroid_Longitude': -78.05760981421811},
 290: {'nombre': 'LA CHORRERA',
  'longitud': -78.31506737,
  'latitud': -6.95005723,
  'UBIGEO': 60309,
  'Dist_Centroid_Latitude': -6.934214997604489,
  'Dist_Centroid_Longitude': -78.29718227998802},
 296: {'nombre': 'SANTA ROSA DE HUASMIN',
  'longitud': -78.31676744,
  'latitud': -6.84836433,
  'UBIGEO': 60304,
  'Dist_Centroid_Latitude': -6.828301323385775,
  'Dist_Centroid_Longitude': -78.32562772354204},
 309: {'nombre': 'CANDEN',
  'longitud': -78.30410337,
  'latitud': -6.45880937,
  'UBIGEO': 60303,
  'Dist_Centroid_Latitude': -6.487693638144822,
  'Dist_Centroid_Longitude': -78.28551672740241},
 313: {'nombre': 'UÑIGAN',
  'longitud': -78.38988366,
  'latitud': -6.5522287,
  'UBIGEO': 60413,
  'Dist_Centroid_Latitude': -6.533926043191311,
  'Dist_Centroid_Longitude': -78.39807832082403},
 320: {'nombre': 'MIGUEL IGLESIAS',
  'longitud': -78.23179707,
  'latitud': -6.64978414,
  'UBIGEO': 60307,
  'Dist_Centroid_Latitude': -6.651037215226543,
  'Dist_Centroid_Longitude': -78.26533736172955},
 326: {'nombre': 'CHUMUCH',
  'longitud': -78.19837123,
  'latitud': -6.60346578,
  'UBIGEO': 60302,
  'Dist_Centroid_Latitude': -6.570579571483123,
  'Dist_Centroid_Longitude': -78.18705795503254},
 329: {'nombre': 'LA LIBERTAD DE PALLAN',
  'longitud': -78.28244339,
  'latitud': -6.72624062,
  'UBIGEO': 60312,
  'Dist_Centroid_Latitude': -6.708803299227402,
  'Dist_Centroid_Longitude': -78.28608939873769},
 332: {'nombre': 'CALCONGA',
  'longitud': -78.15580885,
  'latitud': -7.00941955,
  'UBIGEO': 60310,
  'Dist_Centroid_Latitude': -7.018749889169237,
  'Dist_Centroid_Longitude': -78.1713049795272},
 339: {'nombre': 'LA QUINUA',
  'longitud': -78.05464005,
  'latitud': -7.07242673,
  'UBIGEO': 60308,
  'Dist_Centroid_Latitude': -7.059288980121174,
  'Dist_Centroid_Longitude': -78.06133279004958},
 341: {'nombre': 'JOSE GALVEZ',
  'longitud': -78.13082021,
  'latitud': -6.9295306,
  'UBIGEO': 60306,
  'Dist_Centroid_Latitude': -6.919895067629336,
  'Dist_Centroid_Longitude': -78.15877251548687},
 342: {'nombre': 'JORGE CHAVEZ',
  'longitud': -78.09407559,
  'latitud': -6.94080369,
  'UBIGEO': 60305,
  'Dist_Centroid_Latitude': -6.93951656771669,
  'Dist_Centroid_Longitude': -78.06949244345488},
 344: {'nombre': 'PUESTO DE SALUD POMARONGO',
  'longitud': -78.06887621,
  'latitud': -7.41290977,
  'UBIGEO': 61005,
  'Dist_Centroid_Latitude': -7.406084236084644,
  'Dist_Centroid_Longitude': -78.05979325503557},
 345: {'nombre': 'PUESTO DE SALUD CHANCAY',
  'longitud': -78.12366049,
  'latitud': -7.39182615,
  'UBIGEO': 61002,
  'Dist_Centroid_Latitude': -7.409440519501964,
  'Dist_Centroid_Longitude': -78.11730721333672},
 347: {'nombre': 'LA GRAMA',
  'longitud': -78.12878775,
  'latitud': -7.46432594,
  'UBIGEO': 61003,
  'Dist_Centroid_Latitude': -7.441246652965817,
  'Dist_Centroid_Longitude': -78.14339863956327},
 348: {'nombre': 'PUESTO DE SALUD SHIRAC',
  'longitud': -78.0465152,
  'latitud': -7.34880313,
  'UBIGEO': 61006,
  'Dist_Centroid_Latitude': -7.35601711617239,
  'Dist_Centroid_Longitude': -78.02097097011375},
 352: {'nombre': 'LICLICONGA',
  'longitud': -77.91045089,
  'latitud': -7.27189469,
  'UBIGEO': 61007,
  'Dist_Centroid_Latitude': -7.226790322303838,
  'Dist_Centroid_Longitude': -77.96818616001592},
 360: {'nombre': 'CENTRO MEDICO SAN MARCOS TU SALUD EN NUESTRAS MANOS EIRL',
  'longitud': -78.1706939,
  'latitud': -7.332226,
  'UBIGEO': 61001,
  'Dist_Centroid_Latitude': -7.329158689680376,
  'Dist_Centroid_Longitude': -78.17123693582491},
 361: {'nombre': 'CONSULTORIO ODONTOLOGICO SAN MARCOS',
  'longitud': -78.1706939,
  'latitud': -7.332226,
  'UBIGEO': 61001,
  'Dist_Centroid_Latitude': -7.329158689680376,
  'Dist_Centroid_Longitude': -78.17123693582491},
 364: {'nombre': 'PUESTO DE SALUD ULLILLIN',
  'longitud': -78.13598169,
  'latitud': -7.25759535,
  'UBIGEO': 61004,
  'Dist_Centroid_Latitude': -7.222072177482906,
  'Dist_Centroid_Longitude': -78.161240434848},
 370: {'nombre': 'MATARA',
  'longitud': -78.26372,
  'latitud': -7.25651,
  'UBIGEO': 60110,
  'Dist_Centroid_Latitude': -7.272018782157795,
  'Dist_Centroid_Longitude': -78.25947777718508},
 374: {'nombre': 'CHANSHAPAMPA',
  'longitud': -78.0304932,
  'latitud': -7.63233824,
  'UBIGEO': 60201,
  'Dist_Centroid_Latitude': -7.631450909366323,
  'Dist_Centroid_Longitude': -78.03214219886948},
 381: {'nombre': 'ALGAMARCA',
  'longitud': -78.24673724,
  'latitud': -7.60726053,
  'UBIGEO': 60202,
  'Dist_Centroid_Latitude': -7.567687463603137,
  'Dist_Centroid_Longitude': -78.25332891600632},
 389: {'nombre': 'OTUTO',
  'longitud': -78.06980719,
  'latitud': -7.52745935,
  'UBIGEO': 60203,
  'Dist_Centroid_Latitude': -7.530122419973073,
  'Dist_Centroid_Longitude': -78.09113520240439},
 395: {'nombre': 'SAN JUAN DE LLUCHUBAMBA',
  'longitud': -77.8806691,
  'latitud': -7.46378668,
  'UBIGEO': 60204,
  'Dist_Centroid_Latitude': -7.474385184657658,
  'Dist_Centroid_Longitude': -77.90631489984364},
 401: {'nombre': 'NANCHOC',
  'longitud': -79.24141148,
  'latitud': -6.95981345,
  'UBIGEO': 61108,
  'Dist_Centroid_Latitude': -7.00851074303496,
  'Dist_Centroid_Longitude': -79.24777398372792},
 403: {'nombre': 'BOLIVAR',
  'longitud': -79.1791,
  'latitud': -6.97723,
  'UBIGEO': 61102,
  'Dist_Centroid_Latitude': -6.975911467344069,
  'Dist_Centroid_Longitude': -79.16248594340846},
 405: {'nombre': 'EL SAUCE',
  'longitud': -79.12735,
  'latitud': -7.06395,
  'UBIGEO': 61110,
  'Dist_Centroid_Latitude': -7.117978014363154,
  'Dist_Centroid_Longitude': -79.16096822944561},
 408: {'nombre': 'DE APOYO CHILETE',
  'longitud': -78.840105,
  'latitud': -7.22042167,
  'UBIGEO': 60502,
  'Dist_Centroid_Latitude': -7.256344097369151,
  'Dist_Centroid_Longitude': -78.82166177374316},
 410: {'nombre': 'POLAN',
  'longitud': -78.76941805,
  'latitud': -7.15555602,
  'UBIGEO': 61202,
  'Dist_Centroid_Latitude': -7.174980674339086,
  'Dist_Centroid_Longitude': -78.78564955844057},
 414: {'nombre': 'EL PRADO',
  'longitud': -79.00941,
  'latitud': -7.033193,
  'UBIGEO': 61105,
  'Dist_Centroid_Latitude': -7.038039308001461,
  'Dist_Centroid_Longitude': -79.00331840461457},
 420: {'nombre': 'TAYAPAMPA',
  'longitud': -78.89912,
  'latitud': -7.09990667,
  'UBIGEO': 61101,
  'Dist_Centroid_Latitude': -7.077477931138254,
  'Dist_Centroid_Longitude': -78.92235563378821},
 427: {'nombre': 'LIVES',
  'longitud': -79.0414859,
  'latitud': -7.08044523,
  'UBIGEO': 61113,
  'Dist_Centroid_Latitude': -7.118897786310705,
  'Dist_Centroid_Longitude': -79.05419583150723},
 429: {'nombre': 'CATÁN - TANTARICA',
  'longitud': -78.93257437,
  'latitud': -7.2999141,
  'UBIGEO': 60507,
  'Dist_Centroid_Latitude': -7.275309367839426,
  'Dist_Centroid_Longitude': -78.94985074914202},
 435: {'nombre': 'POSTA MEDICA ESSALUD CONTUMAZA',
  'longitud': -78.80451335,
  'latitud': -7.365567,
  'UBIGEO': 60501,
  'Dist_Centroid_Latitude': -7.334746134440747,
  'Dist_Centroid_Longitude': -78.7367953933113},
 436: {'nombre': 'SANTA CRUZ DE TOLEDO',
  'longitud': -78.83687594,
  'latitud': -7.3452637,
  'UBIGEO': 60506,
  'Dist_Centroid_Latitude': -7.327661926670727,
  'Dist_Centroid_Longitude': -78.86339781991325},
 438: {'nombre': 'SAN BENITO',
  'longitud': -78.92762956,
  'latitud': -7.42775777,
  'UBIGEO': 60505,
  'Dist_Centroid_Latitude': -7.502785547355022,
  'Dist_Centroid_Longitude': -78.96231940728968},
 439: {'nombre': 'JAGUEY',
  'longitud': -78.94018339,
  'latitud': -7.61443072,
  'UBIGEO': 131101,
  'Dist_Centroid_Latitude': -7.501507538534319,
  'Dist_Centroid_Longitude': -78.76394795430816},
 441: {'nombre': 'TOTORILLAS',
  'longitud': -78.93001098,
  'latitud': -7.3534676,
  'UBIGEO': 60504,
  'Dist_Centroid_Latitude': -7.364304037451185,
  'Dist_Centroid_Longitude': -78.9176888857091},
 442: {'nombre': 'LA FLORIDA',
  'longitud': -79.12444358,
  'latitud': -6.87032866,
  'UBIGEO': 61106,
  'Dist_Centroid_Latitude': -6.868881427325543,
  'Dist_Centroid_Longitude': -79.14883383226443},
 443: {'nombre': 'NIEPOS',
  'longitud': -79.12840201,
  'latitud': -6.9267286,
  'UBIGEO': 61109,
  'Dist_Centroid_Latitude': -6.926717799126992,
  'Dist_Centroid_Longitude': -79.13143524251483},
 453: {'nombre': 'PABELLON CHICO',
  'longitud': -78.75323,
  'latitud': -6.89316,
  'UBIGEO': 61107,
  'Dist_Centroid_Latitude': -6.891368966467351,
  'Dist_Centroid_Longitude': -78.74868872486786},
 456: {'nombre': 'TANTACHUAL BAJO',
  'longitud': -78.69851,
  'latitud': -6.93277,
  'UBIGEO': 61111,
  'Dist_Centroid_Latitude': -6.931431002406688,
  'Dist_Centroid_Longitude': -78.7252617863822},
 460: {'nombre': 'TAULIS',
  'longitud': -78.98910379,
  'latitud': -6.93800848,
  'UBIGEO': 61103,
  'Dist_Centroid_Latitude': -6.913739691961161,
  'Dist_Centroid_Longitude': -78.9531112880556},
 464: {'nombre': 'SANTA ROSA DE UNANCA',
  'longitud': -78.78178041,
  'latitud': -7.08410971,
  'UBIGEO': 61201,
  'Dist_Centroid_Latitude': -7.086013887615922,
  'Dist_Centroid_Longitude': -78.78578175939919},
 470: {'nombre': 'EL REGALADO',
  'longitud': -78.67149,
  'latitud': -6.95964,
  'UBIGEO': 61204,
  'Dist_Centroid_Latitude': -6.987533031711155,
  'Dist_Centroid_Longitude': -78.67619782431038},
 472: {'nombre': 'SAN LUIS BAJO - GRANDE',
  'longitud': -78.8690618,
  'latitud': -7.1566476,
  'UBIGEO': 61203,
  'Dist_Centroid_Latitude': -7.159045931055743,
  'Dist_Centroid_Longitude': -78.8730841197766},
 477: {'nombre': 'POSTA MEDICA TEMBLADERA',
  'longitud': -79.1325135,
  'latitud': -7.2536261,
  'UBIGEO': 60508,
  'Dist_Centroid_Latitude': -7.277622253053686,
  'Dist_Centroid_Longitude': -79.16912177287334},
 478: {'nombre': 'TRINIDAD',
  'longitud': -79.0265153,
  'latitud': -7.34802389,
  'UBIGEO': 60503,
  'Dist_Centroid_Latitude': -7.373463162233786,
  'Dist_Centroid_Longitude': -79.06656543590603},
 484: {'nombre': 'HUACATAZ',
  'longitud': -78.429987,
  'latitud': -7.078862,
  'UBIGEO': 60108,
  'Dist_Centroid_Latitude': -7.090123121733361,
  'Dist_Centroid_Longitude': -78.45597511219698},
 497: {'nombre': 'YERBA BUENA',
  'longitud': -78.37325155,
  'latitud': -6.99192237,
  'UBIGEO': 60105,
  'Dist_Centroid_Latitude': -6.98522815524193,
  'Dist_Centroid_Longitude': -78.40294104106351},
 509: {'nombre': 'NAMORA',
  'longitud': -78.3233237,
  'latitud': -7.20283723,
  'UBIGEO': 60111,
  'Dist_Centroid_Latitude': -7.184364016375891,
  'Dist_Centroid_Longitude': -78.28407074183642},
 512: {'nombre': 'LLACANORA',
  'longitud': -78.42567616,
  'latitud': -7.19224192,
  'UBIGEO': 60107,
  'Dist_Centroid_Latitude': -7.189598635181219,
  'Dist_Centroid_Longitude': -78.41184104232502},
 514: {'nombre': 'QUILCATE ALTO',
  'longitud': -78.7313,
  'latitud': -6.81692,
  'UBIGEO': 61104,
  'Dist_Centroid_Latitude': -6.8042771604382,
  'Dist_Centroid_Longitude': -78.73384164479626},
 516: {'nombre': 'TONGOD',
  'longitud': -78.82383,
  'latitud': -6.76306,
  'UBIGEO': 61112,
  'Dist_Centroid_Latitude': -6.788161271933613,
  'Dist_Centroid_Longitude': -78.83354722256809},
 526: {'nombre': 'LA TULPUNA',
  'longitud': -78.55323,
  'latitud': -7.06213,
  'UBIGEO': 60101,
  'Dist_Centroid_Latitude': -7.077465094056442,
  'Dist_Centroid_Longitude': -78.56111128956503},
 712: {'nombre': 'ASUNCION',
  'longitud': -78.51852644,
  'latitud': -7.32536372,
  'UBIGEO': 60102,
  'Dist_Centroid_Latitude': -7.338237028825001,
  'Dist_Centroid_Longitude': -78.5271835710878},
 715: {'nombre': 'COSPAN',
  'longitud': -78.54295072,
  'latitud': -7.42671499,
  'UBIGEO': 60104,
  'Dist_Centroid_Latitude': -7.453031861989977,
  'Dist_Centroid_Longitude': -78.51685530281875},
 718: {'nombre': 'CUMBICO',
  'longitud': -78.632315,
  'latitud': -7.213976,
  'UBIGEO': 60109,
  'Dist_Centroid_Latitude': -7.224728505531067,
  'Dist_Centroid_Longitude': -78.62238468653182},
 721: {'nombre': 'SAN JUAN',
  'longitud': -78.49506255,
  'latitud': -7.29234457,
  'UBIGEO': 60112,
  'Dist_Centroid_Latitude': -7.273909973011706,
  'Dist_Centroid_Longitude': -78.48197922947648},
 723: {'nombre': 'EL CARMEN',
  'longitud': -78.32248816,
  'latitud': -7.3008959,
  'UBIGEO': 60106,
  'Dist_Centroid_Latitude': -7.298307992783807,
  'Dist_Centroid_Longitude': -78.36664819792553},
 728: {'nombre': 'CHETILLA',
  'longitud': -78.6737509,
  'latitud': -7.14631393,
  'UBIGEO': 60103,
  'Dist_Centroid_Latitude': -7.142504145663757,
  'Dist_Centroid_Longitude': -78.65402714365003},
 768: {'nombre': 'POLICLINICO DE ESPECIALIDADES MEDICAS SAN FERNANDO',
  'longitud': -78.64573428,
  'latitud': -6.56052723,
  'UBIGEO': 60401,
  'Dist_Centroid_Latitude': -6.559978310714595,
  'Dist_Centroid_Longitude': -78.64275186422964},
 780: {'nombre': 'LA SINRRA',
  'longitud': -78.73645,
  'latitud': -6.57357,
  'UBIGEO': 60410,
  'Dist_Centroid_Latitude': -6.574491015719826,
  'Dist_Centroid_Longitude': -78.72756806509733},
 792: {'nombre': 'PION',
  'longitud': -78.481833,
  'latitud': -6.178873,
  'UBIGEO': 60414,
  'Dist_Centroid_Latitude': -6.18419525009336,
  'Dist_Centroid_Longitude': -78.47818241083328},
 799: {'nombre': 'SEGUES',
  'longitud': -78.82662,
  'latitud': -6.47196,
  'UBIGEO': 60407,
  'Dist_Centroid_Latitude': -6.483027838660707,
  'Dist_Centroid_Longitude': -78.85225970740323},
 806: {'nombre': 'SAN JOSE DE CHIMBAN',
  'longitud': -78.48150874,
  'latitud': -6.29336733,
  'UBIGEO': 60405,
  'Dist_Centroid_Latitude': -6.28458920942883,
  'Dist_Centroid_Longitude': -78.44814198981383},
 809: {'nombre': 'HUAMBOS',
  'longitud': -78.96331205,
  'latitud': -6.45221694,
  'UBIGEO': 60409,
  'Dist_Centroid_Latitude': -6.443692593489247,
  'Dist_Centroid_Longitude': -78.97040145793493},
 817: {'nombre': 'SEXI',
  'longitud': -79.05109564,
  'latitud': -6.56318719,
  'UBIGEO': 61309,
  'Dist_Centroid_Latitude': -6.585582547393862,
  'Dist_Centroid_Longitude': -79.0363532251021},
 827: {'nombre': 'PARAGUAY',
  'longitud': -79.07915777,
  'latitud': -6.36131899,
  'UBIGEO': 60415,
  'Dist_Centroid_Latitude': -6.352218191989084,
  'Dist_Centroid_Longitude': -79.09294326718505},
 838: {'nombre': 'MASINTRANCA',
  'longitud': -78.50506968,
  'latitud': -6.52640318,
  'UBIGEO': 60419,
  'Dist_Centroid_Latitude': -6.517607398542453,
  'Dist_Centroid_Longitude': -78.49523805940689},
 863: {'nombre': 'VIRGEN DEL CARMEN',
  'longitud': -78.5172716,
  'latitud': -6.67878577,
  'UBIGEO': 60701,
  'Dist_Centroid_Latitude': -6.69306296278482,
  'Dist_Centroid_Longitude': -78.47949984132427},
 882: {'nombre': 'LA UNION',
  'longitud': -78.40786,
  'latitud': -6.45139,
  'UBIGEO': 60403,
  'Dist_Centroid_Latitude': -6.439864212778357,
  'Dist_Centroid_Longitude': -78.41279275671727},
 884: {'nombre': 'MANGALPA',
  'longitud': -78.35963,
  'latitud': -6.37859,
  'UBIGEO': 60406,
  'Dist_Centroid_Latitude': -6.370062989700249,
  'Dist_Centroid_Longitude': -78.36904304935265},
 895: {'nombre': 'NUNGO',
  'longitud': -78.54632,
  'latitud': -6.39887,
  'UBIGEO': 60417,
  'Dist_Centroid_Latitude': -6.398356573617845,
  'Dist_Centroid_Longitude': -78.5557038806839},
 903: {'nombre': 'ANGUIA',
  'longitud': -78.60617513,
  'latitud': -6.34102702,
  'UBIGEO': 60402,
  'Dist_Centroid_Latitude': -6.337966578285939,
  'Dist_Centroid_Longitude': -78.57580091635643},
 911: {'nombre': 'TUGUZA',
  'longitud': -78.70047135,
  'latitud': -6.43306323,
  'UBIGEO': 60404,
  'Dist_Centroid_Latitude': -6.42507988722172,
  'Dist_Centroid_Longitude': -78.70803618399547},
 915: {'nombre': 'CHETILLA',
  'longitud': -78.59686376,
  'latitud': -6.47654758,
  'UBIGEO': 60408,
  'Dist_Centroid_Latitude': -6.470609104117881,
  'Dist_Centroid_Longitude': -78.60245166146207},
 923: {'nombre': 'MAICHIL',
  'longitud': -79.18481411,
  'latitud': -6.51372806,
  'UBIGEO': 60411,
  'Dist_Centroid_Latitude': -6.539008709903909,
  'Dist_Centroid_Longitude': -79.17288948460461},
 929: {'nombre': 'MIRACOSTA',
  'longitud': -79.28471454,
  'latitud': -6.40375901,
  'UBIGEO': 60412,
  'Dist_Centroid_Latitude': -6.357816689554456,
  'Dist_Centroid_Longitude': -79.30263128468026},
 936: {'nombre': 'MACUACO',
  'longitud': -79.04854,
  'latitud': -6.75689,
  'UBIGEO': 61303,
  'Dist_Centroid_Latitude': -6.749358435052559,
  'Dist_Centroid_Longitude': -79.07690654224525},
 943: {'nombre': 'LAS PAMPAS',
  'longitud': -79.25237124,
  'latitud': -6.51230518,
  'UBIGEO': 60416,
  'Dist_Centroid_Latitude': -6.484356711582533,
  'Dist_Centroid_Longitude': -79.24551380101464},
 944: {'nombre': 'TOCMOCHE',
  'longitud': -79.36030984,
  'latitud': -6.41197944,
  'UBIGEO': 60418,
  'Dist_Centroid_Latitude': -6.456833769681386,
  'Dist_Centroid_Longitude': -79.37598617970025},
 957: {'nombre': 'POSTA MEDICA DE HUALGAYOC',
  'longitud': -78.60712809,
  'latitud': -6.7640298,
  'UBIGEO': 60703,
  'Dist_Centroid_Latitude': -6.75273178649143,
  'Dist_Centroid_Longitude': -78.6002550951478},
 965: {'nombre': 'PERLAMAYO',
  'longitud': -78.70348,
  'latitud': -6.65466,
  'UBIGEO': 60702,
  'Dist_Centroid_Latitude': -6.685518797196959,
  'Dist_Centroid_Longitude': -78.70768210400887},
 967: {'nombre': 'SANTA CRUZ',
  'longitud': -78.943336,
  'latitud': -6.630679,
  'UBIGEO': 61301,
  'Dist_Centroid_Latitude': -6.627556466156622,
  'Dist_Centroid_Longitude': -78.94191466046652},
 974: {'nombre': 'ANDABAMBA',
  'longitud': -78.81947,
  'latitud': -6.66291,
  'UBIGEO': 61302,
  'Dist_Centroid_Latitude': -6.664009037035801,
  'Dist_Centroid_Longitude': -78.80972226391866},
 977: {'nombre': 'ACHIRAMAYO',
  'longitud': -78.76768,
  'latitud': -6.67817,
  'UBIGEO': 61306,
  'Dist_Centroid_Latitude': -6.695731296328825,
  'Dist_Centroid_Longitude': -78.76868982762464},
 980: {'nombre': 'BAÑOS CHANCAY',
  'longitud': -78.88959899,
  'latitud': -6.55025775,
  'UBIGEO': 61304,
  'Dist_Centroid_Latitude': -6.555963298481807,
  'Dist_Centroid_Longitude': -78.87793669351107},
 986: {'nombre': 'MIRAFLORES',
  'longitud': -78.8365381,
  'latitud': -6.60928969,
  'UBIGEO': 61305,
  'Dist_Centroid_Latitude': -6.62091640748167,
  'Dist_Centroid_Longitude': -78.85937972057158},
 988: {'nombre': 'PULAN',
  'longitud': -78.92323,
  'latitud': -6.73994,
  'UBIGEO': 61307,
  'Dist_Centroid_Latitude': -6.746564193315141,
  'Dist_Centroid_Longitude': -78.92116181232358},
 993: {'nombre': 'SAUCEPAMPA',
  'longitud': -78.916056,
  'latitud': -6.69143542,
  'UBIGEO': 61308,
  'Dist_Centroid_Latitude': -6.682860526811872,
  'Dist_Centroid_Longitude': -78.9084686522991},
 995: {'nombre': 'UTICYACU',
  'longitud': -78.79456398,
  'latitud': -6.60602069,
  'UBIGEO': 61310,
  'Dist_Centroid_Latitude': -6.614700237533157,
  'Dist_Centroid_Longitude': -78.79053750141917},
 999: {'nombre': 'PUCHUDEN',
  'longitud': -78.85784368,
  'latitud': -6.69074851,
  'UBIGEO': 61311,
  'Dist_Centroid_Latitude': -6.682859653400819,
  'Dist_Centroid_Longitude': -78.84943957704252},
 1012: {'nombre': 'FILADELFIA',
  'longitud': -78.91688993,
  'latitud': -6.13276821,
  'UBIGEO': 60602,
  'Dist_Centroid_Latitude': -6.12740892029638,
  'Dist_Centroid_Longitude': -78.93095194829469},
 1023: {'nombre': 'SANTA ROSA DE QUEROCOTILLO',
  'longitud': -79.08397231,
  'latitud': -6.2160125,
  'UBIGEO': 60607,
  'Dist_Centroid_Latitude': -6.196787754579376,
  'Dist_Centroid_Longitude': -79.09163350392943},
 1042: {'nombre': 'MESARRUME',
  'longitud': -78.76281,
  'latitud': -5.94732,
  'UBIGEO': 60603,
  'Dist_Centroid_Latitude': -5.924614771976213,
  'Dist_Centroid_Longitude': -78.77343762794727},
 1048: {'nombre': 'CUNUAT',
  'longitud': -78.54462,
  'latitud': -6.11962,
  'UBIGEO': 60604,
  'Dist_Centroid_Latitude': -6.116751622775737,
  'Dist_Centroid_Longitude': -78.55885649264842},
 1057: {'nombre': 'TAYALES',
  'longitud': -78.65473339,
  'latitud': -6.11793846,
  'UBIGEO': 60613,
  'Dist_Centroid_Latitude': -6.118075749420057,
  'Dist_Centroid_Longitude': -78.67710453728564},
 1094: {'nombre': 'CONDAY',
  'longitud': -78.83738,
  'latitud': -6.38125,
  'UBIGEO': 60601,
  'Dist_Centroid_Latitude': -6.380199143880152,
  'Dist_Centroid_Longitude': -78.83300549536361},
 1133: {'nombre': 'SANTA ROSA DE LA CAPILLA',
  'longitud': -78.8399451,
  'latitud': -6.27235831,
  'UBIGEO': 60612,
  'Dist_Centroid_Latitude': -6.257949078684879,
  'Dist_Centroid_Longitude': -78.84272293558155},
 1142: {'nombre': 'PIMPINCOS',
  'longitud': -78.75668917,
  'latitud': -6.06070669,
  'UBIGEO': 60606,
  'Dist_Centroid_Latitude': -6.07089520466843,
  'Dist_Centroid_Longitude': -78.76829226789994},
 1152: {'nombre': 'LA SACILIA',
  'longitud': -78.697265,
  'latitud': -6.001959,
  'UBIGEO': 60615,
  'Dist_Centroid_Latitude': -5.987986823734805,
  'Dist_Centroid_Longitude': -78.69242406634012},
 1154: {'nombre': 'SAN ANDRES',
  'longitud': -78.70804708,
  'latitud': -6.24053655,
  'UBIGEO': 60608,
  'Dist_Centroid_Latitude': -6.224067513358646,
  'Dist_Centroid_Longitude': -78.7422789712748},
 1163: {'nombre': 'CHACRERIAS',
  'longitud': -78.57731333,
  'latitud': -6.22945167,
  'UBIGEO': 60605,
  'Dist_Centroid_Latitude': -6.230898091099231,
  'Dist_Centroid_Longitude': -78.56969883536429},
 1170: {'nombre': 'SANTA ROSA DE SAN JUAN',
  'longitud': -78.60362,
  'latitud': -6.17102,
  'UBIGEO': 60609,
  'Dist_Centroid_Latitude': -6.174218113399612,
  'Dist_Centroid_Longitude': -78.60009249414134},
 1179: {'nombre': 'MINAS DE SOCOTA',
  'longitud': -78.69043,
  'latitud': -6.29289,
  'UBIGEO': 60614,
  'Dist_Centroid_Latitude': -6.287362417254426,
  'Dist_Centroid_Longitude': -78.69671913212724},
 1189: {'nombre': 'SANTA ROSA DEL TINGO',
  'longitud': -78.59764,
  'latitud': -6.26656,
  'UBIGEO': 60610,
  'Dist_Centroid_Latitude': -6.279101100284,
  'Dist_Centroid_Longitude': -78.57875268121131},
 1191: {'nombre': 'PISAGUAS',
  'longitud': -78.64009333,
  'latitud': -5.24869333,
  'UBIGEO': 10205,
  'Dist_Centroid_Latitude': -4.99126684432514,
  'Dist_Centroid_Longitude': -78.38959521553005}}

7.5.3 Obtaining travel time and distance:¶

In [20]:
# Inserta tu clave de API de Google Maps aquí
API_KEY = 'AIzaSyBNcRvFtOZhMQR7rhx2iB6VPYDXalsugFQ'
gmaps = googlemaps.Client(key=API_KEY)

# Define the departure time as Monday 4pm. 
departure_time = pd.Timestamp('2024-02-26 16:00:00') 

#We select the last Monday of February so that the code can run when it is reviewed 

# Define the traffic models to be used
traffic_models = ["best_guess","pessimistic", "optimistic"]

# Define an empty list to store the results
results = []
for (ind,lis) in list(establish_data.items()):
    
    # Define the origin and destination as latitudes and longitudes
    origin = (lis["latitud"], lis["longitud"])
    destination = (lis["Dist_Centroid_Latitude"], lis["Dist_Centroid_Longitude"])
          
    # Retrieve the travel time and distance for each traffic model
    for traffic_model in traffic_models:
        dist = gmaps.distance_matrix(origin, destination, departure_time=departure_time, 
                                    mode="driving",traffic_model=traffic_model)
            
        temp = dist["rows"][0]["elements"][0]
        try :
            dur_intraffic_opt_output = temp["duration_in_traffic"]["value"]  # duration
            dist_output = temp["distance"]["value"] #distancia
        except Exception as e:
            dur_intraffic_opt_output = 0
            dist_output = 0
            
            
        # Append the result to the results list
            
        results.append({'indice': lis['UBIGEO'],
                        'traffic_model': traffic_model,
                        'travel_time': dur_intraffic_opt_output,
                        'distance': dist_output})
        
time_and_dist = pd.DataFrame(results)

time_and_dist.drop_duplicates(inplace=True)

7.5.4 Results Manipulation¶

A dataFrame named time_and_dist is created from the results, and duplicates are removed.

In [21]:
time_and_dist = pd.DataFrame(results)

time_and_dist.drop_duplicates(inplace=True)

7.5.5 Pivot and rename Columns:¶

In [22]:
# Pivot the DataFrame to obtain the desired columns
pivot_table = time_and_dist.pivot(index='indice', columns='traffic_model', 
                                  values=['travel_time', 'distance'])

# Name the new columns according to the required format
new_columns = []
for col in pivot_table.columns:
    if col[0] == 'distance':
        if col[1] == 'best_guess':
            new_columns.append(f'travel_distance_best_guess')
        elif col[1] == 'optimistic':
            new_columns.append(f'travel_distance_pessimistic')
        elif col[1] == 'pessimistic':
            new_columns.append(f'travel_distance_optimistic')
    else:
        new_columns.append(f'{col[0]}_{col[1]}')

pivot_table.columns = new_columns

# Reset the index to have 'indice' as a column
pivot_table.reset_index(inplace=True)

# Rename the 'indice' column to 'UBIGEO'
pivot_table.rename(columns={'indice': 'UBIGEO'}, inplace=True)

# Display the resulting DataFrame
pivot_table
Out[22]:
UBIGEO travel_time_best_guess travel_time_optimistic travel_time_pessimistic travel_distance_best_guess travel_distance_pessimistic travel_distance_optimistic
0 10205 0 0 0 0 0 0
1 60101 619 619 619 3906 3906 3906
2 60102 844 844 844 5542 5542 5542
3 60103 1573 1573 1573 10590 10590 10590
4 60104 2211 2211 2211 14134 14134 14134
... ... ... ... ... ... ... ...
123 61308 256 256 256 1741 1741 1741
124 61309 189 189 189 1546 1546 1546
125 61310 222 222 222 1486 1486 1486
126 61311 301 301 301 2194 2194 2194
127 131101 10103 10056 10203 107020 107020 107020

128 rows × 7 columns

7.5.6 Reorganize columns in the 'merged_data_2' and 'pivot_table' dataframes:¶

In [23]:
# Select and rearrange columns in merged_data_2
merged_data_2 = merged_data_2[['id_eess', 'codigo_renaes', 'categoria', 'nombre', 'diresa', 'red', 'direccion', 'longitud',
                               'latitud', 'id_ubigeo', 'UBIGEO', 'CCDD', 'NOMBDEP', 'CCPP', 'NOMBPROV', 'CCDI', 'NOMBDIST',
                               'CAPITAL', 'Dist_Centroid_Latitude', 'Dist_Centroid_Longitude']]

# Select and rearrange columns in pivot_table
pivot_table = pivot_table[['UBIGEO', 'travel_time_best_guess', 'travel_time_pessimistic', 'travel_time_optimistic',
                           'travel_distance_best_guess', 'travel_distance_pessimistic', 'travel_distance_optimistic']]

7.5.7 Merged dataframes: final_data¶

In [24]:
final_data = pd.merge(merged_data_2, pivot_table, on=['UBIGEO'],how="left")
final_data
Out[24]:
id_eess codigo_renaes categoria nombre diresa red direccion longitud latitud id_ubigeo ... NOMBDIST CAPITAL Dist_Centroid_Latitude Dist_Centroid_Longitude travel_time_best_guess travel_time_pessimistic travel_time_optimistic travel_distance_best_guess travel_distance_pessimistic travel_distance_optimistic
0 26418 4214 I-1 LAS NARANJAS CAJAMARCA JAEN OTROS COMUNIDAD DE LAS NARANJAS COMUNIDAD DE L... -78.846347 -5.745295 636 ... JAEN JAEN -5.770179 -78.852551 1138 1138 1138 8185 8185 8185
1 24900 4230 I-1 SAN AUGUSTIN CAJAMARCA JAEN CALLE CALLE IVA S/N S/N CALLE IVA S/N BELLAVIS... -78.793840 -5.585960 637 ... BELLAVISTA BELLAVISTA -5.564669 -78.742625 540 592 592 4412 4412 4412
2 17284 4233 I-2 PACHAPIRIANA CAJAMARCA JAEN OTROS COMUNIDAD DE PACHAPIRIANA COMUNIDAD DE P... -79.126397 -5.640227 638 ... CHONTALI CHONTALI -5.633938 -79.133355 0 0 0 0 0 0
3 1918 7017 I-2 AHUYACA CAJAMARCA JAEN OTROS C.P AHUYACA C.P AHUYACA COLASAY JAEN CAJ... -79.002566 -5.935045 639 ... COLASAY COLASAY -5.901052 -78.979456 1324 1324 1324 6256 6256 6256
4 11525 4241 I-3 HUABAL CAJAMARCA JAEN OTROS COMUNIDAD DE HUABAL COMUNIDAD DE HUABAL ... -78.899635 -5.612133 640 ... HUABAL HUABAL -5.604925 -78.905854 311 311 311 2074 2074 2074
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
124 13976 7179 I-1 CHACRERIAS CAJAMARCA SOCOTA OTROS COMUNIDAD DE CHACRERIAS COMUNIDAD DE CHA... -78.577313 -6.229452 622 ... LA RAMADA LA RAMADA -6.230898 -78.569699 25 25 25 201 201 201
125 26339 7103 I-1 SANTA ROSA DE SAN JUAN CAJAMARCA SOCOTA OTROS COMUNIDAD DE SANTA ROSA COMUNIDAD DE SAN... -78.603620 -6.171020 626 ... SAN JUAN DE CUTERVO SAN JUAN DE CUTERVO -6.174218 -78.600092 0 0 0 0 0 0
126 8592 6937 I-1 MINAS DE SOCOTA CAJAMARCA SOCOTA OTROS COMUNIDAD DE MINAS COMUNIDAD DE MINAS SO... -78.690430 -6.292890 631 ... SOCOTA SOCOTA -6.287362 -78.696719 63 62 62 563 563 563
127 16451 6870 I-1 SANTA ROSA DEL TINGO CAJAMARCA SOCOTA OTROS COMUNIDAD DE SANTA ROSA DEL TINGO DISTRI... -78.597640 -6.266560 627 ... SAN LUIS DE LUCMA SAN LUIS DE LUCMA -6.279101 -78.578753 848 848 848 5807 5807 5807
128 26156 6995 I-1 PISAGUAS CAJAMARCA SAN IGNACIO OTROS COMUNIDAD DE PISAGUAS DISTRITO HUARANGO ... -78.640093 -5.248693 650 ... IMAZA CHIRIACO -4.991267 -78.389595 0 0 0 0 0 0

129 rows × 26 columns

In [25]:
final_data.columns
Out[25]:
Index(['id_eess', 'codigo_renaes', 'categoria', 'nombre', 'diresa', 'red',
       'direccion', 'longitud', 'latitud', 'id_ubigeo', 'UBIGEO', 'CCDD',
       'NOMBDEP', 'CCPP', 'NOMBPROV', 'CCDI', 'NOMBDIST', 'CAPITAL',
       'Dist_Centroid_Latitude', 'Dist_Centroid_Longitude',
       'travel_time_best_guess', 'travel_time_pessimistic',
       'travel_time_optimistic', 'travel_distance_best_guess',
       'travel_distance_pessimistic', 'travel_distance_optimistic'],
      dtype='object')

7.6 Make a histogram of travel time colored by hue = traffic_model (best_guess, pessimistic, and optimistic) for each Province.¶

In [26]:
import plotly.express as px

# Select relevant columns for the histogram plot
data_hist_travel = final_data[['id_eess', 'NOMBPROV', 'travel_time_best_guess', 'travel_time_optimistic', 'travel_time_pessimistic']]
# Reshape the DataFrame using the melt function for better visualization
data_hist_travel = data_hist_travel.melt(id_vars=["id_eess", "NOMBPROV"],
                                         var_name="model",  # rename
                                         value_name="travel_time")  # rename

# Convert 'travel_time' column to numeric, handling errors by coercing to NaN
data_hist_travel['travel_time'] = pd.to_numeric(data_hist_travel['travel_time'], errors='coerce')

# Set color mapping for each column
color_discrete_map = {'travel_time_best_guess': 'lightgreen', 'travel_time_optimistic': 'lightblue', 'travel_time_pessimistic': 'lightcoral'}

# Create a histogram plot using Plotly Express
fig = px.histogram(data_hist_travel, x='travel_time', color='model', facet_col='NOMBPROV', nbins=15, facet_col_wrap=3, color_discrete_map=color_discrete_map)
# Customize layout and appearance
fig.update_layout(
    title=dict(text='<b>Tiempo de viaje por provincia</b>', x=0.5, font=dict(size=16)),
    xaxis_title=dict(text='<b>Tiempo de manejo en segundos</b>', standoff=10, font=dict(size=14)),
    yaxis_title=dict(text='<b>Frecuencia</b>', standoff=10, font=dict(size=14)),
    height=800,
    width=1200,
    plot_bgcolor='white'   # Set the background color to white
)

# Show axes with line, ticks, and tick labels
fig.update_xaxes(showline=True, linewidth=2, linecolor='gray', mirror=True, showticklabels=True, tickfont=dict(size=9.5))
fig.update_yaxes(showline=True, linewidth=2, linecolor='gray', mirror=True, showticklabels=True, tickfont=dict(size=9.5))

# Modify individual plot titles
fig.for_each_annotation(lambda a: a.update(text="Provincia=" + a.text.split("=")[-1]))

# Show the final plot
fig.show()

7.7 Make a histogram of travel distance colored by hue = traffic_model (best_guess, pessimistic, and optimistic) for each Province.¶

In [27]:
# Select relevant columns for the histogram plot
data_hist_travel = final_data[['id_eess', 'NOMBPROV', 'travel_distance_best_guess', 'travel_distance_optimistic', 'travel_distance_pessimistic']]
# Reshape the DataFrame using the melt function for better visualization
data_hist_travel = data_hist_travel.melt(id_vars=["id_eess", "NOMBPROV"],
                                         var_name="model",  # rename
                                         value_name="travel_distance")  # rename

# Convert 'travel_time' column to numeric, handling errors by coercing to NaN
data_hist_travel['travel_distance'] = pd.to_numeric(data_hist_travel['travel_distance'], errors='coerce')

# Set color mapping for each column
color_discrete_map = {'travel_distance_best_guess': 'lightgreen', 'travel_distance_optimistic': 'lightblue', 'travel_distance_pessimistic': 'lightcoral'}

# Create a histogram plot using Plotly Express
fig = px.histogram(data_hist_travel, x='travel_distance', color='model', facet_col='NOMBPROV', nbins=15, facet_col_wrap=3, color_discrete_map=color_discrete_map)
# Customize layout and appearance
fig.update_layout(
    title=dict(text='<b>Distancia de viaje por provincia</b>', x=0.5, font=dict(size=16)),
    xaxis_title=dict(text='<b>Distancia de manejo en segundos</b>', standoff=10, font=dict(size=14)),
    yaxis_title=dict(text='<b>Frecuencia</b>', standoff=10, font=dict(size=14)),
    height=800,
    width=1200,
    plot_bgcolor='white'  # Set the background color to white
)

# Show axes with line, ticks, and tick labels
fig.update_xaxes(showline=True, linewidth=2, linecolor='gray', mirror=True, showticklabels=True, tickfont=dict(size=9.5))
fig.update_yaxes(showline=True, linewidth=2, linecolor='gray', mirror=True, showticklabels=True, tickfont=dict(size=9.5))

# Modify individual plot titles
fig.for_each_annotation(lambda a: a.update(text="Provincia=" + a.text.split("=")[-1]))

# Show the final plot
fig.show()

7.8 Bar plot of the mean travel distance by Province colored by hue = traffic_model (best_guess, pessimistic, and optimistic). The x-axis should be the name of the Province.¶

In [28]:
# Select relevant columns for calculating the mean
selected_columns = ['travel_distance_best_guess', 'travel_distance_pessimistic', 'travel_distance_optimistic']

# Calculate the mean distance per province and traffic model
mean_distance_by_province = final_data.groupby('NOMBPROV')[selected_columns].mean().reset_index()

# Melt the DataFrame for better visualization in the bar plot
mean_distance_by_province_melted = mean_distance_by_province.melt(id_vars=['NOMBPROV'],
                                                                   var_name='traffic_model',
                                                                   value_name='mean_travel_distance')

# Create the Bar Plot using Plotly Express
fig = px.bar(mean_distance_by_province_melted, x='NOMBPROV', y='mean_travel_distance',
             color='traffic_model',
             title='<b>                      Distancia media de viaje por provincia y modelo de tráfico                 </b>',
             labels={'mean_travel_distance': '<b>Distancia media de viaje </b>', 'NOMBPROV': '<b>Provincia</b>'},
             color_discrete_map={'travel_distance_best_guess': 'lightgreen',
                                 'travel_distance_pessimistic': 'lightblue',
                                 'travel_distance_optimistic': 'lightcoral'})

# Adjust the layout of the plot
fig.update_layout(
    height=600,
    width=1200,
    plot_bgcolor='white',  # Set the background color to white
    showlegend=True,      # Show the legend
)

# Show axes with line, ticks, and tick labels
fig.update_xaxes(showline=True, linewidth=2, linecolor='gray', mirror=True, showticklabels=True, tickfont=dict(size=9.5))
fig.update_yaxes(showline=True, linewidth=2, linecolor='gray', mirror=True, showticklabels=True, tickfont=dict(size=9.5))

# Show the plot
fig.show()

7.9 Bar plot of the mean travel time by Province colored by hue = traffic_model (best_guess, pessimistic, and optimistic). The x-axis should be the name of the Province.¶

In [29]:
import plotly.express as px

# Select relevant columns for calculating the mean
selected_columns_time = ['travel_time_best_guess', 'travel_time_pessimistic', 'travel_time_optimistic']

# Calculate the mean distance per province and traffic model
mean_time_by_province = final_data.groupby('NOMBPROV')[selected_columns_time].mean().reset_index()

# Melt the DataFrame for better visualization in the bar plot
mean_time_by_province_melted = mean_time_by_province.melt(id_vars=['NOMBPROV'],
                                                           var_name='traffic_model',
                                                           value_name='mean_travel_time')

# Create the Bar Plot using Plotly Express
fig_time = px.bar(mean_time_by_province_melted, x='NOMBPROV', y='mean_travel_time',
                  color='traffic_model',  
                  title='<b>                      Tiempo media de viaje por provincia y modelo de tráfico              </b>',
                  labels={'mean_travel_time': '<b>Tiempo medio de viaje (segundos)</b>', 'NOMBPROV': '<b>Provincia</b>'},
                  color_discrete_map={'travel_time_best_guess': 'lightgreen',
                                      'travel_time_pessimistic': 'lightblue',
                                      'travel_time_optimistic': 'lightcoral'})

# Adjust the layout of the plot
fig_time.update_layout(
    height=600,
    width=1200,
    plot_bgcolor='white',  # Set the background color to white
    showlegend=True,       # Show the legend
)

# Show axes with line, ticks, and tick labels
fig_time.update_xaxes(showline=True, linewidth=2, linecolor='gray', mirror=True, showticklabels=True, tickfont=dict(size=9.5))
fig_time.update_yaxes(showline=True, linewidth=2, linecolor='gray', mirror=True, showticklabels=True, tickfont=dict(size=9.5))

# Show the plot
fig_time.show()

7.10 Make a count plot of the number of health establishments by Department colored by hue = categoria. The x-axis should be the Department Name.¶

In [30]:
# Load the CSV file
salud_data = pd.read_csv(r"../../_data//salud_places_peru.csv")
salud_data
Out[30]:
id_eess codigo_renaes categoria nombre diresa red direccion longitud latitud id_ubigeo
0 5845 1 II-2 HOSPITAL IQUITOS "CESAR GARAYAR GARCIA" LORETO NO PERTENECE A NINGUNA RED CALLE CORNEJO PORTUGAL 1710 IQUITOS MAYNAS LORETO -73.253653 -3.762818 1453
1 15599 2 SD CENTRO REHABILITACION ENFERMO MENTAL LORETO NO PERTENECE A NINGUNA RED CALLE 3 DE MAYO S/N CPM QUILCATACTA -73.323250 -3.731890 1463
2 18282 3 III-1 HOSPITAL REGIONAL DE LORETO "FELIPE SANTIAGO A... LORETO NO PERTENECE A NINGUNA RED AVENIDA AVENIDA 28 DE JULIO S/N S/N AVENIDA 28... -73.253417 -3.726960 1460
3 18156 4 I-3 C.S. I-3 SANTA MARIA DE NANAY LORETO MAYNAS CIUDAD OTROS CASERIO DE SANTA MARIA DE NANAY CASERIO ... -73.696785 -3.888998 1454
4 858 5 I-1 SAMITO LORETO MAYNAS CIUDAD OTROS CASERIO DE SAMITO SIN NUMERO DISTRITO AL... -73.610928 -3.865167 1454
... ... ... ... ... ... ... ... ... ... ...
28837 20228 9999164 I-3 CENTRO DE SALUD MENTAL COMUNITARIO "SAN ISIDRO... PUNO NO PERTENECE A NINGUNA RED NaN 0.000000 0.000000 1719
28838 4292 9999165 I-3 CENTRO DE SALUD MENTAL COMUNITARIO "LA CAPILL... PUNO NO PERTENECE A NINGUNA RED NaN 0.000000 0.000000 1719
28839 5423 9999166 I-3 CENTRO DE SALUD MENTAL COMUNITARIO "SAN MIGUEL... PUNO NO PERTENECE A NINGUNA RED NaN 0.000000 0.000000 1719
28840 28694 9999167 I-3 CENTRO DE SALUD MENTAL COMUNITARIO "CALLAO" CALLAO NO PERTENECE A NINGUNA RED NaN 0.000000 0.000000 690
28841 21087 9999168 I-3 CENTRO DE SALUD MENTAL COMUNITARIO "VILLA LOS ... CALLAO NO PERTENECE A NINGUNA RED NaN 0.000000 0.000000 695

28842 rows × 10 columns

In [31]:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Create the count plot using Seaborn
plt.figure(figsize=(12, 8))
sns.countplot(x='diresa', hue='categoria', data=salud_data)

# Adjust the layout of the count plot
plt.title('Número de Establecimientos de Salud por Departamento y Categoría', fontsize=16)
plt.xlabel('Departamento', fontsize=14)
plt.ylabel('Número de Establecimientos', fontsize=14)
plt.legend(title='Categoría', title_fontsize='14', fontsize='12')
plt.xticks(rotation=45, ha='right')

# Show the count plot
plt.show()